<?php

class AW_Image_Admin_AuthsubController extends Mage_Adminhtml_Controller_Action
{
    public function authAction()
    {
        if ($apiModelCode = $this->getRequest()->getParam('api_model_code')){
            $apiModel = Mage::getSingleton('image/connector')->getApiModel($apiModelCode);
            $requestedResource = 'http://gdata.youtube.com';
            $returnUri = $this->getUrl('image_admin/admin_authsub/return');       
        }
        $this->getResponse()->setRedirect(Zend_Gdata_AuthSub::getAuthSubTokenUri($returnUri, $requestedResource, 0, 1));
    }

    public function returnAction()
    {
        if ($token = $this->getRequest()->getParam('token')){
            $session_token = Zend_Gdata_AuthSub::getAuthSubSessionToken($token);
            Mage::getConfig()->saveConfig('image/youtube/access_token', $session_token);
        }
        $this->_redirect('adminhtml/system_config/edit', array('section'=>'image'));
    }

    public function revokeAction()
    {
        if ($apiModelCode = $this->getRequest()->getParam('api_model_code')){
            if (($connector = Mage::getSingleton('image/connector'))
                        && ($apiModel = $connector->getApiModel($apiModelCode))
                        && $apiModel->logOut())
            {
                Mage::getConfig()->saveConfig('image/youtube/access_token', '');
                Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('image')->__('Token revoked successfull'));
            } else {
                Mage::getSingleton('adminhtml/session')->addError(Mage::helper('image')->__('Token revoking was failed'));
            }
        }                
        $this->_redirect('adminhtml/system_config/edit', array('section'=>'image'));
    }
}
